import copy

import cv2
import numpy as np

def compute_laplacian_variance(image_path):
    # 读取图像
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

    # 计算图像的拉普拉斯变换
    laplacian = cv2.Laplacian(image, cv2.CV_64F)

    # 计算拉普拉斯方差
    laplacian_variance = np.var(laplacian)

    return laplacian_variance


def apply_deeper_blur(image_path, blur_type='gaussian', kernel_size=5, iterations=1):
    # 读取图像
    image = cv2.imread(image_path)

    # 应用多次模糊
    for _ in range(iterations):
        if blur_type == 'gaussian':
            image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
        elif blur_type == 'average':
            image = cv2.blur(image, (kernel_size, kernel_size))
        elif blur_type == 'median':
            image = cv2.medianBlur(image, kernel_size)
        else:
            raise ValueError("Unsupported blur type")

    return image


def apply_black_obstruction(image_path, side='left'):
    # 读取图像
    image = cv2.imread(image_path)

    # 获取图像的高度和宽度
    height, width = image.shape[:2]

    # 创建一个与图像大小相同的全零图像
    black_image = copy.deepcopy(image)

    if side == 'left':
        # 设置图像左半边像素值为0
        black_image[:, :width // 2] = 0
    elif side == 'right':
        # 设置图像右半边像素值为0
        black_image[:, width // 2:] = 0
    else:
        raise ValueError("Invalid side parameter")

    return black_image

# 图像路径
image_path = 'config/hikcam.jpg'
image_path2 = 'config/zhedang.jpeg'
image_path3 = 'config/zhegai2.jpeg'
image_path4 = 'config/zhegai3.jpeg'
image_path_blur = 'config/blurred_image.jpg'
image_path_obstruction = 'config/obstruction_image.jpg'

# 应用高斯模糊
blurred_image = apply_deeper_blur(image_path, blur_type='gaussian', kernel_size=15, iterations=15)
# 保存模糊后的图像
cv2.imwrite(image_path_blur, blurred_image)

# 应用黑色遮挡
black_obstructed_image = apply_black_obstruction(image_path)
cv2.imwrite(image_path_obstruction, black_obstructed_image)

# 全黑色遮挡
fully_black_obstructed_image = apply_black_obstruction(image_path_obstruction, side='right')
image_path_obstruction_fully = 'config/fully_obstruction_image.jpg'
cv2.imwrite(image_path_obstruction_fully, fully_black_obstructed_image)

# 计算拉普拉斯方差
variance = compute_laplacian_variance(image_path)
print("拉普拉斯方差（原图）:", variance)
variance = compute_laplacian_variance(image_path_blur)
print("拉普拉斯方差（模糊）:", variance)
variance = compute_laplacian_variance(image_path_obstruction)
print("拉普拉斯方差（半遮挡）:", variance)
variance = compute_laplacian_variance(image_path_obstruction_fully)
print("拉普拉斯方差（全部遮挡）:", variance)

variance = compute_laplacian_variance(image_path2)
print("拉普拉斯方差（zhedang）:", variance)

variance = compute_laplacian_variance(image_path3)
print("拉普拉斯方差（zhedang3）:", variance)
variance = compute_laplacian_variance(image_path4)
print("拉普拉斯方差（zhedang4）:", variance)



